package com.shuqi.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.shuqi.activity.BookContentActivity;
import com.shuqi.activity.LoginActivity;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.database.dao.impl.BookInfoDao;
import com.shuqi.database.dao.impl.BookMarkInfoDao;
import com.shuqi.database.model.BookInfo;
import com.shuqi.database.model.BookMarkInfo;
import com.shuqi.model.bean.BookContentInfo;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 进入阅读页入口
 * 
 * @author Jianda.Yang
 * @date 2013-12-10 上午11:25:29
 * @version 1.0
 */
public class EnterBookContent {

    /**
     * 通过书签跳转到阅读页
     * 
     * @param context
     * @param info 书签信息
     * @param requestCode
     */
    public static void jumpToBookContentByBookMark(Activity context, BookMarkInfo info,
            int requestCode) {
        Intent intent = getIntentByBookMark(context, info, false);
        if (intent != null) {
            if (requestCode != -1) {
                context.startActivityForResult(intent, requestCode);
            } else {
                context.startActivity(intent);
            }
            StatisticsUtils.onEvent(context, StatisticsEventID.BOOKCONTENT_ENTER_SUCCEED);
        } else {
            LoginActivity.openVerifyForResult(context, LoginActivity.LOGINTYPE_VERIFY);
        }
    }


    /**
     * 通过文件路径,跳转到阅读页
     * 
     * @param context
     * @param filename
     */
    public static void jumpToBookContentByFilePath(Activity context, String filePath,
            int requestCode) {

        if (TextUtils.isEmpty(filePath)) {// 本地书签，没有路径，跳过
            return;
        }
        BookMarkInfo info = BookMarkInfoDao.getInstance().getLocalBookMarkByPath(filePath);
        Log4an.e("yjd", "info is null=" + (info == null ? true : false));
        if (info != null) {
            Log4an.e("yjd",
                    "getFilePath=" + info.getFilePath() + " ,getBookByte=" + info.getBookReadByte()
                            + " ,getBookType=" + info.getBookType());
            jumpToBookContentByBookMark(context, info, requestCode);
        } else {
            // 根据书签信息判断书籍的类型
            info = new BookMarkInfo();
            info.setFilePath(filePath);
            info.setBookType(BookMarkInfo.TYPE_LOCAL_BOOKMARK);
            if (info.getFilePath().trim().toLowerCase().endsWith(".txt")) {
                info.setBookName(Util.getSubNameFromPath(filePath, ".txt"));
                info.setBookReadByte(0);
                info.setPercent(0);
            } else if (info.getFilePath().trim().toLowerCase().endsWith(".epub")) {
                info.setBookReadByte(0);
                info.setBookTotalByte(1);
            } else if (info.getFilePath().trim().toLowerCase().endsWith(".umd")) {
                info.setBookTotalByte(0);
            } else if (info.getFilePath().trim().toLowerCase().endsWith(".sqd.zip")) {
                // 书签初始化
                info.setBookReadByte(0);
                info.setBookTotalByte(1);
            } else {
                Log4an.e("yjd", "the file type is not txt,epub,umd");
                return;
            }
            Log4an.e("yjd", "info =" + info);
            jumpToBookContentByBookMark(context, info, requestCode);

        }
    }

    /**
     * 通过书签获取跳转intent
     * 
     * @param context
     * @param info
     * @param shelf true 从书架跳转
     * @return
     */
    private static Intent getIntentByBookMark(Context context, BookMarkInfo info, boolean shelf) {
        // if (Constant.GUEST_USER_ID.equals(info.getUserId())) {
        // return null;
        // }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        BookContentInfo params = new BookContentInfo();
        params.setBookmarkByteOffset(info.getBookReadByte());
        Log4an.e("yjd", "【阅读页入口】info.getBookType()=" + (info.getBookType()));
        if (BookMarkInfo.TYPE_OLD_ONLINE_BOOKMARK == info.getBookType()) {
            params.setBookname(info.getBookName());
            params.setBookId(info.getBookId());
            params.setCurChapterCid(info.getChapterId());
            params.setType(Constant.BOOKTYPE_PAY);
            params.setImageUrl(info.getBookCoverImgUrl());
            params.setUid(info.getUserId());
            params.setSourceId(info.getSourceId());
            params.setOld(true);
            BookInfo bookInfo =
                    BookInfoDao.getInstance()
                            .getBookInfo(null, params.getBookId(), params.getUid());

            // 兼容7.x升级后的数据
            if (bookInfo == null) {
                params.setNeedBuy(false);
            } else {
                if (BookInfo.DEFAULT_NO_AUTO_BUY == bookInfo.getBookAutoBuyState()
                        || BookInfo.NO_AUTO_BUY == bookInfo.getBookAutoBuyState()) {
                    params.setNeedBuy(false);
                } else if (BookInfo.AUTO_BUY == bookInfo.getBookAutoBuyState()) {
                    params.setNeedBuy(true);
                }
            }
            bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
            intent.putExtras(bundle);
            intent.setClass(context, BookContentActivity.class);
        } else if (BookMarkInfo.TYPE_OLD_BAG_BOOKMARK == info.getBookType()) {
            params.setBookname(info.getBookName());
            params.setCurChapterCid(info.getChapterId());
            params.setPercent1(String.valueOf(info.getPercent()));
            params.setBookId(info.getBookId());
            params.setBookname(info.getBookName());
            params.setCurChapterName(info.getChapterName());
            params.setType(Constant.BOOKTYPE_BAG);
            bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
            intent.putExtras(bundle);
            Log4an.d("yjd", "[Bag入口]params：" + "getBookname=" + params.getBookname() + " \ncid="
                    + params.getCurChapterCid() + " ,chapterName=" + params.getCurChapterName()
                    + " ,getType=" + params.getType());
            intent.setClass(context, BookContentActivity.class);
        } else if (BookMarkInfo.TYPE_LOCAL_BOOKMARK == info.getBookType()) {
            if (info.getFilePath() != null) {
                if (info.getFilePath().trim().toLowerCase().endsWith(".txt")) {
                    params.setBookname(info.getBookName());
                    params.setPercent1(String.valueOf(info.getPercent()));
                    params.setFliePath(info.getFilePath());
                    params.setImageUrl(info.getBookCoverImgUrl());
                    params.setType(Constant.BOOKTYPE_TXT);
                    params.setTotalLength((int) info.getTotalChapter());
                    bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
                    intent.putExtras(bundle);
                    Log4an.d("yjd", "[TXT入口]params：" + "getBookname=" + params.getBookname()
                            + " \ngetFliePath=" + params.getFliePath() + " ,getStartIndex="
                            + params.getStartIndex() + " ,getType=" + params.getType());
                    intent.setClass(context, BookContentActivity.class);
                } else if (info.getFilePath().trim().toLowerCase().endsWith(".umd")) {
                    params.setFliePath(info.getFilePath());
                    params.setBookname(TextUtils.isEmpty(info.getBookName()) ? Util
                            .getSubNameFromPath(info.getFilePath(), ".umd") : info.getBookName());
                    params.setPercent1(String.valueOf(info.getPercent()));
                    params.setType(Constant.BOOKTYPE_UMD);
                    params.setImageUrl(info.getBookCoverImgUrl());
                    Log4an.d("yjd", "[UMD入口]params：" + "getBookname=" + params.getBookname()
                            + " \ngetFliePath=" + params.getFliePath() + " ,getStartIndex="
                            + params.getStartIndex() + " ,getType=" + params.getType()
                            + "\ngetPercent1=" + params.getPercent1());
                    bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
                    intent.putExtras(bundle);
                    intent.setClass(context, BookContentActivity.class);
                } else if (info.getFilePath().trim().toLowerCase().endsWith(".epub")) {
                    params.setCurChapterCid(info.getChapterId());
                    params.setBookname(TextUtils.isEmpty(info.getBookName()) ? Util
                            .getSubNameFromPath(info.getFilePath(), ".epub") : info.getBookName());
                    params.setFliePath(info.getFilePath());
                    params.setImageUrl(info.getBookCoverImgUrl());
                    params.setPercent1(String.valueOf(info.getPercent()));
                    params.setType(Constant.BOOKTYPE_EPUB);
                    Log4an.d("yjd", "[EPUB入口]params：" + "getBookname=" + params.getBookname()
                            + " \ngetFliePath=" + params.getFliePath() + " ,getStartIndex="
                            + params.getStartIndex() + " ,getType=" + params.getType()
                            + "\ngetPercent1=" + params.getPercent1() + ",getCurChapterCid="
                            + params.getCurChapterCid());
                    bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
                    intent.putExtras(bundle);
                    intent.setClass(context, BookContentActivity.class);
                }
            } else {
                return null;
            }
        } else if (BookMarkInfo.TYPE_NEW_SHENMA_BOOKMARK == info.getBookType()) {
            params.setUid(info.getUserId());
            params.setBookId(info.getBookId());
            params.setCurChapterCid(info.getChapterId());
            params.setBookname(info.getBookName());
            params.setType(Constant.BOOKTYPE_NET);
            params.setCurChapterPayMode(info.getPayMode());
            params.setSourceId(info.getSourceId());
            bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
            intent.putExtras(bundle);
            intent.setClass(context, BookContentActivity.class);
            StatisticsUtils.onEvent(StatisticsEventID.BOOKCONTENT_SHENMA_ENTER_READ);
            Log4an.d("yjd", "[神马入口]name=" + params.getBookname() + ",bid=" + params.getBookId()
                    + ",type=" + params.getType() + ",cid=" + params.getCurChapterCid() + ",uid="
                    + params.getUid() + ",sourceID=" + params.getSourceId());
        } else if (BookMarkInfo.TYPE_NEW_PAY_BOOKMARK == info.getBookType()) {
            params.setUid(info.getUserId());
            params.setBookId(info.getBookId());
            params.setTotalLength(info.getTotalChapter());
            params.setCurChapterCid(info.getChapterId());
            params.setBookname(info.getBookName());
            params.setType(Constant.BOOKTYPE_PAY);
            params.setCurChapterPayMode(info.getPayMode());
            params.setOld(false);
            params.setPercent1(info.getPercent() + "");
            params.setImageUrl(info.getBookCoverImgUrl());
            BookInfo bookInfo =
                    BookInfoDao.getInstance()
                            .getBookInfo(null, params.getBookId(), params.getUid());
            if (bookInfo != null) {
                if (BookInfo.DEFAULT_NO_AUTO_BUY == bookInfo.getBookAutoBuyState()
                        || BookInfo.NO_AUTO_BUY == bookInfo.getBookAutoBuyState()) {
                    params.setNeedBuy(true);
                } else if (BookInfo.AUTO_BUY == bookInfo.getBookAutoBuyState()) {
                    params.setNeedBuy(false);
                }
            } else {
                params.setNeedBuy(true);
            }
            Log4an.d(
                    "yjd",
                    "[收费入口]name=" + params.getBookname() + ",bid=" + params.getBookId()
                            + ",length=" + params.getTotalLength() + ",type=" + params.getType()
                            + ",cid=" + params.getCurChapterCid() + ",uid=" + params.getUid()
                            + ",percent=" + params.getPercent1() + "\ngetChapterPayMode="
                            + info.getPayMode() + "\nimgurl=" + info.getBookCoverImgUrl()
                            + "\nneedbuy=" + params.isNeedBuy());
            bundle.putSerializable(Constant.BOOKCONTENT_BUNDLE_KEY, params);
            intent.putExtras(bundle);
            intent.setClass(context, BookContentActivity.class);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return intent;
    }
}
